package com.bkitclub.mobiledevcamp.horseword.customGUI;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Bitmap.Config;

import com.bkitclub.mobiledevcamp.horseword.util.ResourceManager;
import com.bkitclub.mobiledevcamp.horseword.util.Utils;

public class BitmapFont {
	private static BitmapFont instance;
	public static BitmapFont getInstance(Context context){
		if(instance == null)
			instance = new BitmapFont(context);
		return instance;
	}
	private Bitmap alphabeFont,numberFont;
	public static final int FONT_MY_WORD = 0;
	public static final int FONT_YOUR_WORD = 1;
	public static final int FONT_SELECTING_WORD = 2;
	private static final int[] resourceFontNumberHuge = {72,65,68,63,55,62,57,64,65,61};
	private static final int[] resourceFontNumberLarge = {43,38,41,36,34,36,33,39,37,37};
	private int[] resourceFont;
	private Paint paint;
	private BitmapFont(Context context){
		alphabeFont = ResourceManager.getInstance().getImage(context, "play/chars.png");
		numberFont = ResourceManager.getInstance().getImage(context, "play/score-font.png");
		alphabeWidth = alphabeFont.getWidth() / 26;
		alphabeHeight = alphabeFont.getHeight() / 3;
		paint = new Paint();
		createResourceFont();
		
	}
	private void createResourceFont(){
		switch(Utils.getResourceType()){
		case Utils.RESOURCE_TYPE_HUGE:
				resourceFont = resourceFontNumberHuge;
			break;
		case Utils.RESOURCE_TYPE_LARGE:
				resourceFont = resourceFontNumberLarge;
			break;
		case Utils.RESOURCE_TYPE_MEDIUM:
		case Utils.RESOURCE_TYPE_SMALL:
			resourceFont = new int[resourceFontNumberLarge.length];
			for(int i = 0 ; i < resourceFontNumberLarge.length ; i++){
				resourceFont[i] = (int) (Utils.getScale() * resourceFontNumberLarge[i]);
			}
			break;
		}
	}
	private int alphabeWidth,alphabeHeight;
	public int getAlphabeHeight(){
		return alphabeHeight;
	}
	private Bitmap getSingleNumber(char number,int type){
		Bitmap result = null;
		int indexDistance = number - '0';
		int xDistance = 0;
		for(int i = 0 ; i < indexDistance ; i++){
			xDistance += resourceFont[i];
		}
		result = Bitmap.createBitmap(numberFont, xDistance, type * result.getHeight() / 2, resourceFont[indexDistance], result.getHeight() / 2);
		return result;
	}
	public Bitmap getNumber(int number,int type){
		String sNumber = String.valueOf(number);
		Bitmap chars[] = new Bitmap[sNumber.length()];
		int w = 0;
		for(int i = 0 ; i < sNumber.length() ; i++){
			char cNumber = sNumber.charAt(i);
			chars[i] = getSingleNumber(cNumber,type);
			w += chars[i].getWidth();
		}
		Bitmap result = Bitmap.createBitmap(w, numberFont.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(result);
		w = 0;
		for(int i = 0 ; i < chars.length ; i++){
			canvas.drawBitmap(chars[i], w, 0, paint);
			w += chars[i].getWidth();
		}
		return result;
	}
	/**
	 * 
	 * @param font: type of font:means my word, your word, selecting word
	 * @param c: upcase font
	 * @return bitmap of font
	 */
	
	public Bitmap getChar(int font,char c){
		Bitmap result = null;
		switch(font){
		case FONT_MY_WORD:
			result = Bitmap.createBitmap(alphabeFont, (c - 'A') * alphabeWidth , 0, alphabeWidth, alphabeHeight);
			break;
		case FONT_YOUR_WORD:
			result = Bitmap.createBitmap(alphabeFont, (c - 'A') * alphabeWidth, alphabeHeight, alphabeWidth, alphabeHeight);
			break;
		case FONT_SELECTING_WORD:
			result = Bitmap.createBitmap(alphabeFont, (c - 'A') * alphabeWidth, 2 * alphabeHeight, alphabeWidth, alphabeHeight);
			break;
		}
		return result;
	}
	public Bitmap getString(int font,String s){
		Bitmap result = null;
		Bitmap chars[] = new Bitmap[s.length()];
		for(int i = 0 ; i < s.length() ; i++){
			chars[i] = getChar(font, s.charAt(i));
		}
		result = Bitmap.createBitmap(chars.length * alphabeWidth, alphabeHeight, Config.ARGB_8888);
		Canvas canvas = new Canvas(result);
		int w = 0;
		for(int i = 0 ; i < chars.length ; i++){
			canvas.drawBitmap(chars[i], w, 0, paint);
			w += chars[i].getWidth();
		}
		return result;
	}
}
